package com.xoms.admin.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.alibaba.fastjson.JSON;
import com.xoms.admin.constants.Rescode;
import com.xoms.admin.constants.RescodeConstants;
import com.xoms.admin.controller.res.BaseResult;
import com.xoms.admin.controller.res.bean.RAccountLoginBean;
import com.xoms.admin.controller.res.bean.RHaveResourcesBean;
import com.xoms.admin.model.AccountBean;
import com.xoms.admin.persist.dao.AccountDao;
import com.xoms.admin.service.AccountService;
import com.xoms.admin.service.ResourceService;
import com.xoms.admin.service.exception.ServiceException;
import com.xoms.admin.service.res.PageResult;
import com.xoms.util.NumUtil;

public class AccountInterceptor extends HandlerInterceptorAdapter {
	Logger logger = Logger.getLogger(this.getClass());
	
	@Autowired
	AccountService accountServiceImpl;

	@Autowired
	ResourceService resourcesServiceImpl;

	@Autowired
	AccountDao accountImpl;

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json");
		BaseResult result = new BaseResult();
		// 获取token
		String token = request.getHeader("token");
		if (token == null) {
			token = request.getParameter("token");
		}
		logger.info("account token is:" + token);
		try {
			RAccountLoginBean account = accountServiceImpl.getAccountByToken(token);
			if (account != null) {
				AccountBean acc = accountImpl.selectOneByAccid(account.getAccId());
				int deptId = NumUtil.toInt(acc.getDeptId(), 0);
				if(deptId == 34) {
					Rescode rescode = RescodeConstants.getInstance().get("account_is_not_allow");
					result.setFlag(false);
					result.setRescode(rescode.getCode());
					result.setMsg(rescode.getMsg());

					response.getWriter().write(JSON.toJSONString(result));
					return false;
					
				}
				// 验证权限地址
				PageResult<RHaveResourcesBean> resources = resourcesServiceImpl.getAccountResources(null, account.getAccId());
				request.setAttribute("resources", resources.getData());
				logger.info("resources is :" + resources.getData());
				
//				if(null == resources || resources.size() == 0) {
//					// 无权限
//					Rescode rescode = RescodeConstants.getInstance().get("account_has_no_auth");
//					result.setFlag(false);
//					result.setRescode(rescode.getCode());
//					result.setMsg(rescode.getMsg());
//	
//					response.setCharacterEncoding("utf-8");
//					response.setContentType("application/json");
//					response.getWriter().write(JSON.toJSONString(result));
//					return false;
//				}
				request.setAttribute("accid", account.getAccId());
				request.setAttribute("account", acc);
				
	
				logger.info("account accid is :" + account.getAccId());
			} else {
				Rescode rescode = RescodeConstants.getInstance().get("token_fail");
				result.setFlag(false);
				result.setRescode(rescode.getCode());
				result.setMsg(rescode.getMsg());

				response.getWriter().write(JSON.toJSONString(result));
				return false;
			}
		} catch (ServiceException e) {
			result.setServiceException(e);
			result.setFlag(false);
			result.setRescode(e.getServiceCode());
			result.setMsg(e.getServiceMsg());
			response.getWriter().write(JSON.toJSONString(result));
			return false;
		}

		return super.preHandle(request, response, handler);
	}	
	

	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		super.afterCompletion(request, response, handler, ex);
	}
}
